<?php

namespace App\Http\Controllers\Front\Ocean;

use App\Common\Controllers\Front\FrontController;
use App\Common\Enums\AdvClickSourceEnum;
use App\Common\Enums\ConvertTypeEnum;
use App\Common\Enums\ExceptionTypeEnum;
use App\Common\Enums\MaterialTypeEnums;
use App\Common\Enums\PlatformEnum;
use App\Common\Enums\ProductTypeEnums;
use App\Common\Services\ErrorLogService;
use App\Common\Services\SystemApi\AdvOceanApiService;
use App\Common\Services\SystemApi\AdvOceanV2ApiService;
use App\Common\Tools\CustomException;
use App\Common\Tools\CustomLock;
use App\Common\Tools\CustomRedis;
use App\Models\Ocean\OceanAccountModel;
use App\Models\Ocean\OceanSpiAccountModel;
use App\Models\Ocean\OceanSpiLogModel;
use App\Services\Ocean\OceanService;
use Illuminate\Http\Request;

class IndexController extends FrontController
{
    /**
     * constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param Request $request
     * @return mixed
     * spi
     */
    public function spi(Request $request){
        $data = $request->all();
        $content = [
            'BaseResp' => [
                "StatusCode" =>  0,
                "StatusMessage" =>  "ok"
            ],
        ];

        if(isset($data['event']) && $data['event'] == 'verify_webhook'){
            $content['Challenge'] = intval($data['challenge']) ?? 0;
            return response()->json($content, 200, []);
        }

        $publishTime = $data['publish_time'] ?? TIMESTAMP;
        $datetime = date('Y-m-d H:i:s', TIMESTAMP);
        if(strlen($publishTime) == 13){
            $publishTime = intval($publishTime / 1000);
        }
        $publishAt = date('Y-m-d H:i:s', $publishTime);
        $publishDate = date('Y-m-d', $publishTime);

        $messageId = $data['message_id'] ?? '';
        $serviceLabel = $data['service_label'] ?? '';

        $oceanSpiAccountModel = new OceanSpiAccountModel();
        $oceanSpiAccounts = [];
        foreach($data['advertiser_ids'] as $advertiserId){
            $oceanSpiAccounts[] = [
                'publish_date' => $publishDate,
                'service_label' => $serviceLabel,
                'account_id' => $advertiserId,
                'created_at' => $datetime,
                'updated_at' => $datetime,
            ];
        }
        $oceanSpiAccountModel->setUpdateIgnoreFields(['created_at']);
        $oceanSpiAccountModel->batchInsertOrUpdate($oceanSpiAccounts);

        $oceanSpiLogModel = new OceanSpiLogModel();
        $oceanSpiLogModel->message_id = $messageId;
        $oceanSpiLogModel->service_label = $serviceLabel;
        $oceanSpiLogModel->subscribe_task_id = $data['subscribe_task_id'] ?? '';
        $oceanSpiLogModel->publish_time = $publishAt;
        $oceanSpiLogModel->extends = $data;
        $oceanSpiLogModel->save();

        return response()->json($content, 200, []);
    }

    /**
     * @param Request $request
     * @return mixed
     * 测试
     */
    public function test(Request $request){
        $key = $request->input('key');
        if($key != 'aut'){
            return $this->forbidden();
        }

        $this->testConvertMatch();
    }

    private function testConvertMatch(){
        $converts = [
            [
                'convert_type' => ConvertTypeEnum::PAY, // 转化类型
                'convert_id' => 10350742, // 转化id
                'convert_at' => '2023-04-10 00:03:24', // 转化时间
                'convert_times' => 1, // 转化次数(包含当前转化)
                "amount" => 39,
                'request_id' => '',
                'muid' => '',
                'oaid' => '',
                'oaid_md5' => '',
                'ip' => '39.144.67.167',
                'ua' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148',
                //'ua' => '',
                // 联运用户信息
                'n8_union_user' => [
                    'guid' => 71742082,
                    'channel_id' => 71629,
                    'created_at' => '2023-04-10 00:00:02',
                    'click_source' => [AdvClickSourceEnum::ADV_CLICK_API],
                    'product_type' => 'WECHAT_MINI_PROGRAM',
                ],
            ],
        ];
        $a = new AdvOceanV2ApiService();
        $ret = $a->apiConvertMatch($converts);
        dd($ret, 'testConvertMatch');
    }

}
